 #!/usr/bin/python
 #coding:utf-8

import time
import json
import requests
import datetime

# cookies = {
#     'PHPSESSID': 'ef4290ac1fd336dedc4d0e1557dc62cd',
# }

headers = {
    'Host': 'www.hfjstt.com',
    'accept': '*/*',
    'content-type': 'application/x-www-form-urlencoded',
    'user-agent': 'iPhone10,3(iOS/13.2.3) Uninview(Uninview/1.0.0) Weex/0.26.0 1125x2436',
    'accept-language': 'zh-cn',
}



def print_response(response):
    try:
        data = json.loads(response.content)
        print(json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
    except Exception as e:
        print('json数据格式不正确：' + str(e))




def post(url,cookies,data):

    response = requests.post(url, headers=headers, cookies=cookies, data=data)
    print_response(response)
    return response


def login(mobile):

    data = 'mobile='+mobile+'&password=21cffaf95fbcc4fc863d700d0004db83&token=21cffaf95fbcc4fc863d700d0004db8396478e506e0171e219c1b4dfebe5fd451584494398469&uuid=%7B%22imei%22%3A%22%20%22%2C%22imsi%22%3A%5B%5D%2C%22uuid%22%3A%220651E70A-54C4-49B7-8F6F-B612E056A4C3%22%7D&ukey=%7B%22errMsg%22%3A%22getSystemInfo%3Aok%22%2C%22brand%22%3A%22Apple%22%2C%22model%22%3A%22iPhoneX%22%2C%22pixelRatio%22%3A3%2C%22screenWidth%22%3A375%2C%22screenHeight%22%3A812%2C%22windowWidth%22%3A375%2C%22windowHeight%22%3A812%2C%22statusBarHeight%22%3A44%2C%22language%22%3A%22zh-Hans-CN%22%2C%22system%22%3A%2213.2.3%22%2C%22version%22%3A%221.9.9.74105%22%2C%22fontSizeSetting%22%3A%22%22%2C%22platform%22%3A%22ios%22%2C%22SDKVersion%22%3A%222.6.1%22%2C%22windowTop%22%3A0%2C%22windowBottom%22%3A0%2C%22safeArea%22%3A%7B%22left%22%3A0%2C%22right%22%3A375%2C%22top%22%3A44%2C%22bottom%22%3A812%2C%22width%22%3A375%2C%22height%22%3A768%7D%2C%22safeAreaInsets%22%3A%7B%22top%22%3A44%2C%22right%22%3A0%2C%22bottom%22%3A0%2C%22left%22%3A0%7D%7D&ukeyy=96478e506e0171e219c1b4dfebe5fd45&uimei=%20&uimsi=&uuuid=0651E70A-54C4-49B7-8F6F-B612E056A4C3'
    url = 'https://www.hfjstt.com/index.php/index/User/login'

    session = requests.Session()
    response = session.post(url, headers=headers, data=data)
    print_response(response)
    return response


def userweixin(userid,cookies):

    data = 'userid='+userid+'&useruid=1'
    url = 'https://www.hfjstt.com/index.php/index/user/userweixin'

    return post(url, cookies, data)



def userweixinyanz(userid,cookies):

    data = 'userid='+userid+'&useruid=1'
    url = 'https://www.hfjstt.com/index.php/index/user/userweixinyanz'
    return post(url, cookies, data)



def spdataa(cookies):
    # 主任务视频
    data = 'page=0'
    url = 'https://www.hfjstt.com/index.php/index/index/spdataa'
    return post(url, cookies, data)


def spdata(cookies):
    # 次任务视频
    data = 'page=0'
    url = 'https://www.hfjstt.com/index.php/index/index/spdata'
    return post(url, cookies, data)



def mryanzhng(userid,cookies):
    # 获取验证码
    data = 'userid='+userid+'&useruid=1'
    url = 'https://www.hfjstt.com/index.php/index/index/mryanzhng'

    return post(url, cookies, data)



def complete(userid,taskId,code,cookies):
    # 输入验证码 完成任务
    data = 'userid='+userid+'&useruid=1'+'&num='+code+'&id='+taskId
    url = 'https://www.hfjstt.com/index.php/index/index/complete'

    return post(url, cookies, data)

def byteify(input, encoding='utf-8'):
    if isinstance(input, dict):
        return {byteify(key): byteify(value) for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [byteify(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode(encoding)
    else:
        return input

def finishTask(mobile):

    response = login(str(mobile))

    cookies = requests.utils.dict_from_cookiejar(response.cookies)
    print cookies

    rs1 = json.loads(response.content)

    loginData = str(rs1['data'])
    userid = loginData + '96478e506e0171e219c1b4dfebe5fd45' + '1584496895638'

    userweixin(userid, cookies)
    userweixinyanz(userid, cookies)

    videos = []
    mainVideo = spdataa(cookies)
    rs2 = json.loads(mainVideo.content)
    videos.append(str(rs2['data']['id']))

    otherVideo = spdata(cookies)
    rs3 = json.loads(otherVideo.content)
    for data in rs3['data']:
        videos.append(str(data['id']))

    for i in range(0, videos.__len__()):
        taskId = videos[i]
        # 获取验证码
        codeResp = mryanzhng(userid, cookies)
        rs2 = json.loads(codeResp.content)
        code = str(rs2['data'])
        # print '获取到验证码：' + code
        time.sleep(1)

        print str(mobile) + ' 输入验证码：' + code
        # 完成任务
        complete(userid, taskId, code, cookies)

        print str(mobile) + ' 完成第 ' + str(i+1) +' 题'

        time.sleep(2)



    # for taskId in videos:
    #     # 获取验证码
    #     codeResp = mryanzhng(userid, cookies)
    #     rs2 = json.loads(codeResp.content)
    #     code = str(rs2['data'])
    #     # print '获取到验证码：' + code
    #     time.sleep(3)
    #
    #     print str(mobile) + ' 输入验证码：' + code
    #     # 完成任务
    #     complete(userid, taskId, code, cookies)
    #
    #     time.sleep(5)

    print str(mobile) + ' 完成任务'

if __name__ == '__main__':

        finishTask(13760354726)
        time.sleep(3)

        finishTask(19875688893)
        time.sleep(3)

        finishTask(18620330995)
        time.sleep(3)

        finishTask(19925481032)
        time.sleep(3)

        finishTask(13144835915)
        time.sleep(3)

        finishTask(19925480679)

        print(datetime.datetime.now())


